package com.share.example.sbh.init;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OpenBrowser {
    @Autowired
    private Environment environment;

    @EventListener({ApplicationReadyEvent.class})
    public void applicationReadyEvent() {
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            log.info("应用已经准备就绪,即将打开浏览器...");
            String port = environment.getProperty("server.port");
            String path = environment.getProperty("server.servlet.context-path");
            String url = "http://localhost:" + port + path + "/index";
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            log.info("当前系统是linux");
        }
    }
}